package com.myWeb.controller;

import com.myWeb.entity.Question;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class ExamServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{

        javax.servlet.http.HttpSession session = request.getSession(false);
        List<Question> questionList = (List<Question>) session.getAttribute("key");
        int score = 0;
        for (Question question : questionList) {
            Integer questionId = question.getQuestionId();
            String answer = question.getAnswer();
            String userAnswer = request.getParameter("answer_" + questionId);
            if(userAnswer != null){
                if (userAnswer.equals(answer)) {
                    score += 20;
                }
            }else{
                request.setAttribute("info", "尚未完成答卷，请返回继续作答");
                request.getRequestDispatcher("/info.jsp").forward(request, response);
            }
        }
        request.setAttribute("info", "本次考试成绩为：" + score + "分!");
        request.getRequestDispatcher("/info.jsp").forward(request, response);

    }
}
